package com.ruoyi.bookkeeping.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bookkeeping.domain.BookAsset;
import com.ruoyi.bookkeeping.domain.param.AddBookSortParam;
import com.ruoyi.bookkeeping.domain.param.UpdateBookSortParam;
import com.ruoyi.bookkeeping.domain.vo.BookSortVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bookkeeping.mapper.BookSortMapper;
import com.ruoyi.bookkeeping.domain.BookSort;
import com.ruoyi.bookkeeping.service.IBookSortService;

/**
 * 分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-24
 */
@Service
public class BookSortServiceImpl extends ServiceImpl<BookSortMapper,BookSort> implements IBookSortService
{
    @Autowired
    private BookSortMapper bookSortMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询分类
     * 
     * @param sortId 分类主键
     * @return 分类
     */
    @Override
    public BookSortVo selectBookSortBySortId(Long sortId,Long userId)
    {
        LambdaQueryWrapper<BookSort> bookSortLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookSortLambdaQueryWrapper.eq(BookSort::getSortId,sortId)
                .eq(BookSort::getUserId,userId);
        BookSort bookSort = baseMapper.selectOne(bookSortLambdaQueryWrapper);
        BookSortVo bookSortVo = new BookSortVo();
        BeanUtils.copyProperties(bookSort,bookSortVo);
        return bookSortVo;
    }

    /**
     * 查询分类列表
     * @param userId
     * @return
     */
    @Override
    public List<BookSortVo> selectBookSortList(Long userId)
    {
        LambdaQueryWrapper<BookSort> bookSortLambdaQueryWrapper = new LambdaQueryWrapper<BookSort>();
        bookSortLambdaQueryWrapper.eq(BookSort::getUserId,userId);
        List<BookSort> bookSorts = baseMapper.selectList(bookSortLambdaQueryWrapper);
        List<BookSortVo> bookSortVoList = new ArrayList<>();
        for(BookSort bookSort:bookSorts){
            BookSortVo bookSortVo = new BookSortVo();
            BeanUtils.copyProperties(bookSort,bookSortVo);
            bookSortVo.setSortName(bookSort.getSortName());
            bookSortVoList.add(bookSortVo);
        }
        return bookSortVoList;
    }

    /**
     * 新增分类
     * @param addBookAssetParam
     * @param userId
     * @return
     */
    @Override
    public int insertBookSort(AddBookSortParam addBookAssetParam,Long userId)
    {
        BookSort bookSort = new BookSort();
        BeanUtils.copyProperties(addBookAssetParam,bookSort);
        bookSort.setCreateTime(DateUtils.getNowDate());
        bookSort.setUserId(userId);
        bookSort.setDelFlag("0");
        bookSort.setCreateBy(sysUserMapper.selectUserById(userId).getUserName());
        return baseMapper.insert(bookSort);
    }

    /**
     * 修改分类
     * @param updateBookSortParam
     * @return
     */
    @Override
    public int updateBookSort(UpdateBookSortParam updateBookSortParam,Long userId)
    {
        if(!ObjectUtil.equal(userId,baseMapper.selectById(updateBookSortParam.getSortId()).getUserId())){
            return 0;
        }
        BookSort bookSort = new BookSort();
        BeanUtils.copyProperties(updateBookSortParam,bookSort);
        bookSort.setCreateTime(DateUtils.getNowDate());
        bookSort.setUpdateBy(sysUserMapper.selectUserById(userId).getUserName());
        bookSort.setUpdateTime(DateUtils.getNowDate());
        return bookSortMapper.updateBookSort(bookSort);
    }

    /**
     * 批量删除分类
     *
     * @param sortIds 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteBookSortBySortIds(Long[] sortIds,Long userId)
    {
        for (Long id : sortIds) {
            LambdaUpdateWrapper<BookSort> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(BookSort::getSortId,id)
                    .eq(BookSort::getUserId,userId);
            if(!ObjectUtil.equal(userId,baseMapper.selectById(id).getUserId())){
                return 0;
            }
        }
        return baseMapper.deleteBatchIds(Arrays.asList(sortIds));
    }

}
